package com.example.springboot.controller.user;

import com.example.springboot.entity.Res;
import com.example.springboot.entity.UserSearchRemember;
import com.example.springboot.entity.vo.UserSearchRememberVo;
import com.example.springboot.service.UserSearchRememberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userSearchRemember")
public class UserSearchRememberController {

    @Autowired
    private UserSearchRememberService userSearchRememberService;

    @PostMapping("/add")
    public Res<String> addUserSearchRemember(@RequestBody UserSearchRemember userSearchRemember) {
        try {
            int result = userSearchRememberService.addUserSearchRemember(userSearchRemember);
            if (result > 0) {
                return new Res<String>().code(200).msg("Add success").data("Success");
            } else {
                return new Res<String>().code(500).msg("Add failed").data("Failure");
            }
        } catch (Exception e) {
            return new Res<String>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }

    @DeleteMapping("/delete/{id}")
    public Res<String> deleteUserSearchRemember(@PathVariable int id) {
        try {
            int result = userSearchRememberService.deleteUserSearchRemember(id);
            if (result > 0) {
                return new Res<String>().code(200).msg("Delete success").data("Success");
            } else {
                return new Res<String>().code(500).msg("Delete failed").data("Failure");
            }
        } catch (Exception e) {
            return new Res<String>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }

    @PutMapping("/update")
    public Res<String> updateUserSearchRemember(@RequestBody UserSearchRemember userSearchRemember) {
        try {
            int result = userSearchRememberService.updateUserSearchRemember(userSearchRemember);
            if (result > 0) {
                return new Res<String>().code(200).msg("Update success").data("Success");
            } else {
                return new Res<String>().code(500).msg("Update failed").data("Failure");
            }
        } catch (Exception e) {
            return new Res<String>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }

    @GetMapping("/get/{id}")
    public Res<UserSearchRemember> getUserSearchRememberById(@PathVariable int id) {
        try {
            UserSearchRemember result = userSearchRememberService.getUserSearchRememberById(id);
            if (result != null) {
                return new Res<UserSearchRemember>().code(200).msg("Get success").data(result);
            } else {
                return new Res<UserSearchRemember>().code(404).msg("Not found").data(null);
            }
        } catch (Exception e) {
            return new Res<UserSearchRemember>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }

    @GetMapping("/getByUserId/{userId}")
    public Res<List<UserSearchRemember>> getUserSearchRememberByUserId(@PathVariable int userId) {
        try {
            List<UserSearchRemember> result = userSearchRememberService.getUserSearchRememberByUserId(userId);
            if (result != null && !result.isEmpty()) {
                return new Res<List<UserSearchRemember>>().code(200).msg("Get success").data(result);
            } else {
                return new Res<List<UserSearchRemember>>().code(404).msg("No records found").data(result);
            }
        } catch (Exception e) {
            return new Res<List<UserSearchRemember>>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }
    @GetMapping("/getHotSearch")
    public Res<List<UserSearchRememberVo>> getHotSearch() {
        try {
            List<UserSearchRememberVo> result = userSearchRememberService.findHotSearch();
            if (result != null && !result.isEmpty()) {
                return new Res<List<UserSearchRememberVo>>().code(200).msg("Get success").data(result);
            } else {
                return new Res<List<UserSearchRememberVo>>().code(404).msg("No records found").data(result);
            }
        } catch (Exception e) {
            return new Res<List<UserSearchRememberVo>>().code(500).msg("Error occurred: " + e.getMessage());
        }
    }
}